/*
 * $Id$
 */
package py.windmill.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @author myang
 */
public class DefaultRequestMapper implements RequestMapper {

    private List<Route> _routes = new ArrayList();

    public static RequestMapper of(final Route... routes) {
        return new DefaultRequestMapper().add(routes);
    }

    /**
     * 
     * @param req
     * @param defaultHandler
     * @return 
     */
    @Override
    public Route map(final Request req) {
        final String uri = req.requestUri();
        for (final Route rt : _routes) {
            if (rt.containsPathParameters()) {
                if (uri.startsWith(rt.path())) {
                    return rt;
                }
            }
            else if (rt.path().equals(uri)) {
                return rt;
            }
        }
        return null;
    }

    public RequestMapper add(final Route... routes) {
        _routes.addAll(Arrays.asList(routes));
        return this;
    }
}
